package com.team.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.team.entity.Document;
import com.team.entity.Project;
import com.team.util.DbHelp;
import com.team.util.ResultSetPackge;

public class DocumentDao {
	DbHelp<Document> db = new DbHelp<Document>();
	
	public int saveDocument(Document doc){
		String sql = "insert into t_document(title,context,createtime,t_user_id,t_project_id)values(?,?,?,?,?)";
		return db.executeUpdateSql(sql, doc.getTitle(),doc.getContext(),doc.getCreateTime(),doc.getUserId(),doc.getProjId());
	}
	
	public List<Document> selectAllDocsByProjId(int id){
		String sql = "select id,title,context,createtime,t_user_id,t_project_id from t_document where t_project_id = ?";
		return db.exeSelectForList(sql, new  PackgeDocument(),id);
		
	}
	
	private class PackgeDocument implements ResultSetPackge<Document>{

		public Document packgeResultSet(ResultSet rs) throws SQLException {
			Document doc = new Document();
			doc.setId(rs.getInt("id"));
			doc.setCreateTime(rs.getString("createtime"));
			doc.setContext(rs.getString("context"));
			doc.setTitle(rs.getString("title"));
			doc.setUserId(rs.getInt("t_user_id"));
			doc.setProjId(rs.getInt("t_project_id"));
			return doc;
		}
		
	}

	public Document selectDocumentById(int id) {
		String sql = "select id,title,context,createtime,t_user_id,t_project_id from t_document where id = ?";
		return db.exeSelectForObject(sql, new PackgeDocument(), id);
	}

	public int updatDocById(String name, String content, int id) {
		String sql = "update t_document set title=?,context=? where id=? ";
		return db.executeUpdateSql(sql, name,content, id);
	}
} 
